package com.ziyia.wim.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ziyia.wim.util.HttpUtil;
import com.ziyia.wim.R;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegisterActivity extends BaseActivity {
    private Button register;
    private EditText username, password, email;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_register);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        email = findViewById(R.id.email);
        register = findViewById(R.id.register);

        register.setOnClickListener(v -> {
            String name = this.username.getText().toString();
            String pass = this.password.getText().toString();
            String email = this.email.getText().toString();


            if ("".equals(name) || "".equals(pass) || "".equals(email)) {
                Toast.makeText(this, "请确保每项不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            String url = "http://" + IP + "/user/register/?username=" + name + "&password=" + pass + "&email=" + email;
            HttpUtil.get(url, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    String resp = Objects.requireNonNull(response.body()).string();

                    JSONObject jsonObject = JSON.parseObject(resp);

                    Map<String, Object> map = jsonObject.getInnerMap();

                    // 状态
                    Integer code = (Integer) map.get("code");
                    String msg = (String) map.get("msg");
                    // 返回的信息
                    Looper.prepare();
                    if (0 == code) {
                        Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        intent.putExtra("user", msg);
                        startActivity(intent);
                    } else {
                        Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                }
            });
        });
    }


    public void login(View view) {
        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(intent);
    }
}